import {
    hold2bit
} from '../components/number.js';

'use strict';
// 初始化交易记录视图
function initView() {
    $("#fanhui").on("touchstart", function() {
        location.href = "./staffHome.html";
    });
    $("#billText").on("touchstart", showMerBox);
    $(".filter_list .filter_close").on("touchstart", hideMerBox);
}
// 创建交易记录
function createRecord(tradeObjects, $parent) {
    var typeName = {
        "01": {
            "btn_name": "01",
            "operType": ""
        },
        "02": {
            "btn_name": "02",
            "operType": ""
        },
        "退货": {
            "btn_name": "退货",
            "operType": "weui-btn weui-btn_plain-primary"
        },
        "完成": {
            "btn_name": "完成",
            "operType": "weui-btn weui-btn_plain-default weui-btn_plain-disabled"
        },
        "退货中": {
            "btn_name": "05",
            "operType": "weui-btn weui-btn_plain-default weui-btn_plain-disabled"
        }
    }
    var nowDate = new Date();
    var themId = "0";
    $parent.empty();
    for (var i = 0, l = tradeObjects.length; i < l; i++) {
        var tradeObject = tradeObjects[i];
        var objectMonthId = parseInt(String(tradeObject.trans_date).substr(4, 2)) - 1;
        if (objectMonthId == nowDate.getMonth() && themId == "0") {
            var tradeTitleHtml = `
            <div class="weui-cells__title">${String(tradeObject.trans_date).substr(4, 2)}月</div>
            `
            $parent.append(tradeTitleHtml);
            themId = "1";
        } else if (objectMonthId == (nowDate.getMonth() - 1) && themId == "1") {
            var tradeTitleHtml = `
            <div class="weui-cells__title">${String(tradeObject.trans_date).substr(4, 2)}月</div>
            `
            $parent.append(tradeTitleHtml);
            themId = "2";
        } else if (objectMonthId == (nowDate.getMonth() - 2) && themId == "2") {
            var tradeTitleHtml = `
            <div class="weui-cells__title">${String(tradeObject.trans_date).substr(4, 2)}月</div>
            `
            $parent.append(tradeTitleHtml);
            themId = "3";
        }
        tradeObject.time = changeDateTime(tradeObject.trans_date, "second");
        //        tradeObject.oper = typeName[tradeObject.trans_type].btn_name;
        tradeObject.operType = typeName[tradeObject.trans_type].operType;
        var trans_amt2bit = hold2bit(tradeObject.trans_amt);
        var mer_name_abbr = tradeObject.mer_abbr?tradeObject.mer_abbr:tradeObject.mer_name;
        var template = `
            <div data-trans_seq="${tradeObject.trans_seq}" class="weui-cell">
                <div class="weui-cell__bd">
                    <p class="trade_name">${mer_name_abbr}</p>
                    <p class="trade_time"><span>${tradeObject.time}</span></p>
                </div>
                <div class="weui-cell__ft">
                    <p class="trade_money">${trans_amt2bit}</p>
                    <p class="trade-type">${tradeObject.trans_status}</p>
                </div>
            </div>`;

        $parent.append(template);
    }

}

// 创建商户列表
function createshLis(shArray) {
    var $shlist = $("#shlist");
    var shLiHtml = `
        <li data-merNo= "">全部</li>
        `;

        $shlist.append(shLiHtml);
    for (var i = 0, l = shArray.length; i < l; i++) {
        var merObject = shArray[i];
        var shLiHtml = `
        <li data-merNo= "${merObject.merNo}">${merObject.merName}</li>
        `;

        $shlist.append(shLiHtml);
    }
}

// 显示商户列表
function showMerBox(erroMsg) {
    $("#merBox").css({
        "opacity": "1",
        "display": ""
    });

}
// 隐藏商户列表
function hideMerBox() {
    setTimeout(function  () {
        $("#merBox").fadeOut(200);
    },200);
    
}


// 时间20161108160023 转为 2016-11-08 16:00:23
function changeDateTime(dateString, type) {
    dateString = String(dateString);
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}


export {
    initView,
    createRecord,
    createshLis
}